﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class TagDesigner : CellTypeDesigner<Tag>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new TagDrawingObject(drawingHelper, this.CellType, cellInfo);
        }

        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            for (int i = 0; i < 6; i++)
            {
                this.CellType.ColorList.Add(new ColorItem()
                {
                    color = "Accent " + (i + 1),
                });
            }
            this.CellType.separator = ",";
            this.CellType.size = "small";
            this.CellType.theme = "default";
            this.CellType.gutter = 10;
            this.CellType.addButtonSettings.width = 100;
            this.CellType.addButtonSettings.text = Resources.Tag_AddButtonText_DefaultValue;
            this.CellType.addButtonSettings.space = 20d;
        }
    }

    [Designer("Vant.CellTypes.TagDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Tag.png")]
    [OrderWeight((int)Utils.OrderWeight.Tag)]
    public class Tag : VantCellTypeBase, ISupportUIPermission, ISupportDefaultValue, ISupportReadOnly
    {
        [SRDisplayName(nameof(Resources.Tabs_EditClickCommand))]
        [SRCustomCommandObject(InitParamProperties = "itemName", InitParamValues = nameof(Resources.Tag_ClickCommand_InitParamValues))]
        public object ClickCommand { get; set; }

        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [SRDisplayName(nameof(Resources.Tag_ConifgColorList))]
        [ListProperty(MinCount = 1)]
        [SRDescription(nameof(Resources.Tag_ColorList_Description))]
        public List<ColorItem> ColorList { get; set; } = new List<ColorItem>();

        [FormulaProperty]
        [SRDisplayName(nameof(Resources.Vant_DefaultValue))]
        public object DefaultValue { get; set; }

        [Browsable(false)]
        [JsonIgnore]
        public bool NeedFormatDefaultValue => false;

        [SRDisplayName(nameof(Resources.Tag_Separator))]
        [ComboProperty(ValueList = ",|/|>", IsSelectOnly = false)]
        public string separator { get; set; }

        [SRDisplayName(nameof(Resources.Tag_Size))]
        [SRComboProperty(ValueList = "small|medium|large", DisplayList = nameof(Resources.Tag_Size_DisplayList))]
        public string size { get; set; }

        [SRDisplayName(nameof(Resources.Tag_Theme))]
        [SRComboProperty(ValueList = "default|plain|round|mark", DisplayList = nameof(Resources.Tag_Theme_DisplayList))]
        public string theme { get; set; }

        [IntProperty(Min = 0)]
        [SRDisplayName(nameof(Resources.Tag_Gutter))]
        public int gutter { get; set; }

        [BoolProperty]
        [DefaultValue(true)]
        [SRDisplayName(nameof(Resources.Tag_AllowAdd))]
        public bool allowAdd { get; set; } = true;

        [SRDisplayName(nameof(Resources.Tag_AddButtonSetting))]
        [ObjectProperty(ObjType = typeof(AddButtonSettings), IndentLevel = 1)]
        public AddButtonSettings addButtonSettings { get; set; } = new AddButtonSettings();

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Tag_Distinct))]
        [SRDescription(nameof(Resources.Tag_Distinct_Description))]
        [DefaultValue(true)]
        public bool distinct { get; set; } = true;

        [DefaultValue(false)]
        [SRDisplayName(nameof(Resources.Vant_Readonly))]
        public bool ReadOnly { get; set;} = false;

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(addButtonSettings))
            {
                return this.allowAdd;
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        public override string ToString()
        {
            return Resources.Tag_DisplayName;
        }
    }

    public class ColorItem
    {
        [SRDisplayName(nameof(Resources.Tag_Color))]
        [ColorProperty]
        [Required]
        public string color { get; set; }
    }

    public class AddButtonSettings : ObjectPropertyBase
    {
        [SRDisplayName(nameof(Resources.Tag_AddButtonWidth))]
        [IntProperty(Min = 10)]
        public int width { get; set; }

        [SRDisplayName(nameof(Resources.Tag_AddButtonText))]
        public string text { get; set; }

        [SRDisplayName(nameof(Resources.Tag_AddButtonGutter))]
        [DoubleProperty(Min = 0)]
        public double space { get; set; }
    }
}
